package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DictInfoDAO;

import com.et.mvc.JsonView;

/**
 * Servlet implementation class selectDictServlet
 */
public class selectDictServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public selectDictServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String type = request.getParameter("type");
        int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();	
		
		DictInfoDAO getDictDAO = new DictInfoDAO();
		@SuppressWarnings("unchecked")
		List getDictSelectList = null;
        Map<String,Object> map = new HashMap<String,Object>();
        if(name.equals("") && type.equals(""))
             {
        	     getDictSelectList = getDictDAO.getAllDict(start, limit);       	         	 
             }           
             else
             {
            	 getDictSelectList = getDictDAO.getDictByQuery(start, limit, name, type);      	
             }
        map.put("total",getDictDAO.count);
        map.put("result",getDictSelectList);  
        
        out.print(new JsonView(map));		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
